<template>
  <div class="limit-analysis">
    <LimitInfo :limitUpData="limitUpData" :defaultDate="defaultDate" @updateDate="updateDate" />
    <template v-if="limitUpData">
      <PlateHistory :tableData="tableData" />
    </template>
  </div>
</template>

<script>
import LimitInfo from '@/components/limitDetail/limitAnalysis/LimitInfo.vue';
import PlateHistory from '@/components/limitDetail/limitAnalysis/PlateHistory.vue';
import { reqZtfx, reqLimitupInfo } from '@/apis/index.js';
import { errorReport, convertNumber, formatDate } from '@/scripts/tools.js';
export default {
  components: {
    LimitInfo,
    PlateHistory,
  },
  data() {
    return {
      limitUpdateArr: [],
      limitUpData: {},
      defaultDate: null,
      date: '',
      // 数据源
      tableData: [],
    };
  },
  methods: {
    getBoardNum() {
      return reqLimitupInfo({ date: this.date, stock_code: window.getParam('stockcode') })
        .then(res => {
          const { stockContinueLimitUpDays } = res.data;
          return stockContinueLimitUpDays;
        })
        .catch(err => {
          errorReport({
            name: `获取连板数接口报错，${err.name}`,
            message: err.message,
            stack: err.stack,
          });
        });
    },
    getLimitUpData() {
      const result = this.limitUpdateArr.find(item => item.date === this.date);
      if (!result) {
        this.$emit('updateStockInfo', '', '');
        this.limitUpData = null;
        return;
      }
      const { ztyy, ztgl, lbzs, jlr, flb, time1, time2, spj, zdf, cje, hsl, zsz, cjl } = result;
      this.$emit('updateStockInfo', spj, zdf);
      this.limitUpData = [
        [
          {
            name: '涨停原因',
            value: ztyy,
            needFull: true,
          },
          {
            name: '明日涨停概率',
            value: ztgl,
          },
          {
            name: '连板数',
            value: lbzs,
          },
          {
            name: '龙虎榜净流入',
            value: convertNumber(jlr, 'hundredMillion'),
            isGreenRed: true,
          },
          {
            name: '封流比',
            value: `${flb}%`,
          },
          {
            name: '首次涨停',
            value: time1,
          },
          {
            name: '最终涨停',
            value: time2,
          },
        ],
        [
          {
            name: '当日收盘价',
            value: spj,
          },
          {
            name: '涨跌幅',
            value: `${zdf}%`,
            isGreenRed: true,
          },
          {
            name: '成交额',
            value: convertNumber(cje, 'hundredMillion'),
            isGreenRed: true,
          },
          {
            name: '换手率',
            value: hsl,
          },
          {
            name: '总市值',
            value: zsz,
          },
          {
            name: '成交量',
            value: convertNumber(cjl, 'tenThousand'),
          },
          {
            name: '流通市值',
            value: convertNumber(cjl, 'hundredMillion'),
          },
        ],
      ];
    },
    // 连板数
    getZtfx(boardNum) {
      const params = {
        stockcode: window.getParam('stockcode'),
        date: this.date,
        size: boardNum,
      };
      reqZtfx(params)
        .then(res => {
          const result = res.result;
          this.limitUpdateArr = result.map(
            ({ ztyy, ztgl, jlr, flb, time1, time2, spj, zdf, cje, hsl, zsz, cjl, ltsz, date }) => ({
              ztyy,
              ztgl,
              // 此处的连板数要来自上一个接口的传参，原先接口连板数会记录三天两板的情况
              lbzs: boardNum,
              jlr,
              flb,
              time1,
              time2,
              spj,
              zdf,
              cje,
              hsl,
              zsz,
              cjl,
              ltsz,
              date,
            })
          );
          this.getLimitUpData();
          const tableData = [];
          result.forEach((item, index) => {
            const { date, jlr, ztyy,wkb } = item;
            tableData.push({
              limitupDate: !date ? '--' : formatDate(date, '-'),
              // 此处的连板数要来自上一个接口的传参的计算，原先接口连板数会记录三天两板的情况
              limitupNum: boardNum - index,
              inflow: convertNumber(jlr, 'hundredMillion'),
              reason: ztyy,
              wkb,
            });
          });
          this.tableData = tableData;
        })
        .catch(err => {
          errorReport({
            name: `涨停分析接口报错，${err.name}`,
            message: err.message,
            stack: err.stack,
          });
        });
    },
    updateDate({ date, defaultDate }) {
      this.date = date;
      this.defaultDate = defaultDate;
      this.getBoardNum().then(boardNum => {
        this.getZtfx(boardNum || 1);
      });
    },
  },
  mounted() {
    this.date = window.getParam('date');
    if (this.date) {
      this.defaultDate = new Date(formatDate(this.date, '/'));
    }
    this.getBoardNum().then(boardNum => {
      this.getZtfx(boardNum || 1);
    });
  },
};
</script>
